<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="0">
          <a-col :md="6" :sm="24">
            <a-form-item label="场景名称">
              <a-input v-model="queryParam.sceneName" allow-clear placeholder="请输入场景名称"/>
            </a-form-item>
          </a-col>
          <a-col :md="(!advanced && 8) || 24" :sm="24">
            <span
              class="table-page-search-submitButtons"
              style="float: left;padding-left: 16px;"
              :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
            >
              <a-button type="primary" class="btn_search_reset" @click="$refs.table.refresh(true)"><a-icon
                type="search"/>查询</a-button>
              <a-button class="btn_search_reset" style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
            </span>
          </a-col>
          <a-col :md="(!advanced && 10) || 24" :sm="24">
            <span
              class="table-page-search-submitButtons"
              style="float: right;"
              :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
            >
              <a-button
                class="btn_add"
                type="primary"
                @click="$refs.addForm.add()"
                icon="plus">新增场景</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <s-table
      ref="table"
      size="default"
      :columns="columns"
      :data="loadData"
      :alert="true"
      :rowKey="(record) => record.id"
      id="table-wrapper"
    >
      <span slot="action" slot-scope="text, record">
        <a class="btn_edit" @click="$refs.editForm.edit(record)">编辑</a>
        <a-divider type="vertical" />
        <a-dropdown>
          <a class="btn_more"> 更多 <a-icon type="down"/></a>
          <a-menu slot="overlay">
            <a-menu-item>
              <a @click="$refs.configForm.init(record)" class="btn_edit">配置</a>
            </a-menu-item>
            <a-menu-item>
              <a-popconfirm placement="topRight" title="确认要同步此场景下所有网关的配置吗？" @confirm="() => sync(record)">
                <a>同步</a>
              </a-popconfirm>
            </a-menu-item>
            <a-menu-item>
              <a-popconfirm placement="topRight" title="确认删除？" @confirm="() => del(record)">
                <a>删除</a>
              </a-popconfirm>
            </a-menu-item>
          </a-menu>
        </a-dropdown>
      </span>
    </s-table>

    <add-form ref="addForm" @ok="handleOk" />
    <edit-form ref="editForm" @ok="handleOk" />
    <config-form ref="configForm" @ok="handleOk" />
  </a-card>
</template>

<script>
import { STable } from '@/components'
import { page, del, syncConfig } from '@/api/modular/main/gatewayscene/gatewayscene'
import addForm from './addForm'
import editForm from './editForm'
import configForm from './configForm'

export default {
  components: {
    STable,
    addForm,
    editForm,
    configForm
  },

  data () {
    return {
      advanced: false, // 高级搜索 展开/关闭
      queryParam: {}, // 查询参数
      columns: [
        // 表头
        {
          title: '场景名称',
          dataIndex: 'sceneName',
          width: 300,
          ellipsis: true
        },
        {
          title: '备注',
          dataIndex: 'remark',
          ellipsis: true
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          width: 220,
          align: 'center'
        },
        {
          title: '操作',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' },
          width: 180,
          align: 'center'
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        return page(Object.assign(parameter, this.queryParam)).then((res) => {
          return res.data
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
      defaultExpandedKeys: [],
      replaceFields: {
        key: 'id'
      }
    }
  },

  methods: {
    del (record) {
      del(record)
        .then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('删除失败：' + res.message)
          }
        })
        .catch((err) => {
          this.$message.error('删除错误：' + err.message)
        })
    },
    sync (record) {
      syncConfig(record)
        .then((res) => {
          if (res.success) {
            this.$message.success('同步成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('同步失败：' + res.message)
          }
        })
        .catch((err) => {
          this.$message.error('同步时发生错误：' + err.message)
        })
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    handleOk () {
      this.$refs.table.refresh()
    }
  }
}
</script>

<style lang="less">
.table-operator {
  margin-bottom: 18px;
}
button {
  margin-right: 8px;
}
</style>
